<!--
 * @Author: 孙开源 && sunkaiyuan@namenode.cn
 * @Date: 2023-09-22 10:26:19
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-09-24 15:04:52
 * @Description: 
 * 
-->
<template>
  <el-dialog
    draggable
    overflow
    v-model="visible"
    :title="titleMap[mode]"
    :width="500"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <sk-tabs type="border-card">
      <el-tab-pane ref="tab" label="基础信息">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px">
          <el-row :gutter="40" justify="space-evenly">
            <el-col :span="24">
              <el-form-item label="单号" prop="code">
                <el-input
                  v-model="form.code"
                  readonly
                  placeholder="自动生成"
                  class="readonly"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="收款日期" prop="billdate">
                <el-date-picker
                  v-model="form.billdate"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="经手人" prop="employee_id">
                <skAutoChoose
                  v-model="form.employee.name"
                  :name="'user'"
                  :asKey="'employee_id'"
                  :assign="form"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="付款单位" prop="company_id">
                <skAutoChoose
                  v-model="form.company.name"
                  :name="'customer'"
                  :asKey="'company_id'"
                  :assign="form"
                  @select="companySelect"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="部门" prop="department_id">
                <sk-select-group
                  v-model="form.department_id"
                  :url="$API.system.financeDepartment.treelist"
                ></sk-select-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="父科目">
                <sk-select-group v-model="parent_subject_id" :list="parent_subjectList" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="科目" prop="subject_id">
                <sk-select-group v-model="form.subject_id" :list="children_subjectList" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="收款账号" prop="account_id">
                <sk-select-group v-model="form.account_id" :list="accountList" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="收款金额" prop="amount">
                <el-input v-model="form.amount"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否含税" prop="tax_id">
                <el-switch
                  v-model="form.tax_id"
                  :active-value="1"
                  :inactive-value="0"
                ></el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="摘要" prop="summary">
                <el-input
                  v-model="form.summary"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                  type="textarea"
                  placeholder="默认：收款-往来单位 金额"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="附件">
        <sk-upload
          v-model="form.attachments"
          :limit="10"
          draggable
          tip="单个文件不要超过10M,请上传图像格式文件"
        >
        </sk-upload>
      </el-tab-pane>
    </sk-tabs>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skSelectGroup from "@/components/skSelectGroup";
import skDicSelect from "@/components/skDicSelect";
import skAutoChoose from "@/components/skAutoChoose";
import XEUtils from "xe-utils";
export default {
  emits: ["success", "closed"],
  components: { skSelectGroup, skDicSelect, skAutoChoose },
  data() {
    return {
      mode: "add",
      titleMap: {
        add: "新增",
        edit: "编辑",
      },
      accountList: [],
      subjectList: [],
      parent_subject_id: null,
      form: {
        id: "",
        billdate: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
        employee: {},
        company: {},
        department: {},
        company_id: -1,
        trade: {},
        code: "",
        rate: 1,
        attachments: [],
        tax_id: 0,
      },
      rules: {
        billdate: { required: true, message: "请选择单据日期" },
        employee_id: { required: true, message: "请选择经手人" },
        account_id: { required: true, message: "请选择付款账户" },
        subject_id: { required: true, message: "请选择分类科目" },
        amount: { required: true, message: "请填写付款金额" },
      },
      visible: false,
      isSaveing: false,
    };
  },
  watch: {
    parent_subject_id() {
      this.form.subject_id = null;
    },
  },
  computed: {
    parent_subjectList() {
      return this.subjectList.map(v => {
        return { id: v.id, name: v.name };
      });
    },
    children_subjectList() {
      let data = this.subjectList.filter(v => {
        return this.parent_subject_id == null || v.id == this.parent_subject_id;
      });
      return data;
    },
  },
  mounted() {
    this.getSubject();
    this.getAccount();
    let userInfo = this.$TOOL.data.get("USER_INFO");
    this.form.employee.name = userInfo.username;
    this.form.employee_id = userInfo.id;
  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;

      this.visible = true;

      return this;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async valid => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.finance.income.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    getAccount() {
      this.$API.basedata.subject.list
        .get({ filter: [{ parent_id: { value: [1002, 1012], type: "in" } }] })
        .then(res => {
          this.accountList = res.data;
        });
    },
    getSubject() {
      this.$API.basedata.subject.menulist.get().then(res => {
        this.subjectList = this.recursionSetName(res.data);
      });
    },
    recursionSetName(v) {
      v = v.map(v => {
        v.name = "├" + v.code + "-" + v.name;
        v.children = this.recursionSetName(v.children);
        return v;
      });
      return v;
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, this.$TOOL.removeNull(data));
    },
  },
};
</script>
<style></style>
